package com.data.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.config.dao.ChargeConfigDao;
import com.config.dao.EquipmentDao;
import com.config.dao.LineConfigDao;
import com.config.entity.ChargeConfig;
import com.config.entity.Equipment;
import com.config.entity.LineConfig;
import com.data.dao.BaseDataDao;
import com.data.dao.ChargeDayDao;
import com.data.dao.ChargeMinDao;
import com.data.entity.ChargeDay;
import com.data.entity.ChargeMin;
@Service("chargeDayService")
public class ChargeDayServiceImpl implements ChargeDayService{
	@Resource(name="baseDataDao")
	private BaseDataDao baseDataDao;
	@Resource(name="lineConfigDao")
	private LineConfigDao lineConfigDao;
	@Resource(name="equipmentDao")
	private EquipmentDao equipmentDao;
	@Resource(name="chargeConfigDao")
	private ChargeConfigDao chargeConfigDao;
	@Resource(name="chargeMinDao")
	private ChargeMinDao chargeMinDao;
	@Resource(name="chargeDayDao")
	private ChargeDayDao chargeDayDao;
	/**
	 * 保存设备每天电费数据
	 * @param belongDate
	 */
	@Override
	public void saveChargeDay(Date belongDate) {
		List<LineConfig> lines=lineConfigDao.findAllLine();
			if(lines.size()!=0){
				for(LineConfig line:lines){
				//	this.saveLineChargeDay(line.getId(),belongDate);  生产线不提取数据
					List<Equipment> equs=equipmentDao.listByLine(line.getId());
					if(equs.size()!=0){
						for(Equipment e:equs){
							this.saveEquChargeDay(e.getId(), belongDate);
					}
				}
			}
		 }
 }
	
	/**
	 * 提取生产线每天电费数据
	 * @param lineId
	 * @param belongDate
	 */
//	private void saveLineChargeDay(int lineId,Date belongDate){
//		try{
//				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//				SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd");
//				List<ChargeConfig>configs=chargeConfigDao.findDistinctChargesName();
//				if(configs.size()!=0){
//					for(ChargeConfig charge:configs){
//						ChargeDay day=chargeDayDao.getChargeDayByDate(lineId, charge.getChargeName(),sdft.format(belongDate),"Y");
//						boolean isNew=false;
//						if(day==null){
//							day=new ChargeDay();
//							day.setLineId(lineId);
//							day.setBelongDate(sdft.parse(sdft.format(belongDate)));
//							day.setChargeName(charge.getChargeName());
//							isNew=true;
//						}
//						//每天开始时间默认为0点，0点30分表示从0点开始算起
//						String startDate=sdft.format(belongDate)+" "+"00:00:00";
//						String endDate=sdf.format(belongDate);
//						List<ChargeMin> list=chargeMinDao.getChargeMinsByDate(lineId, startDate, endDate,charge.getChargeName(),"Y");
//						double relativeComp=0.0;double charges=0.0;
//						if(list.size()!=0){
//							for(ChargeMin min:list){
//								relativeComp+=min.getRelativeComp();
//								charges+=min.getCharge();
//							}
//						}
//						day.setRelativeComp(relativeComp);
//						day.setCharge(charges);
//						if(isNew){
//							chargeDayDao.save(day);
//						}else{
//							chargeDayDao.update(day);
//					}
//			}
//		}
//	} catch (ParseException e1) {
//			e1.printStackTrace();
//  }
//}
	/**
	 * 提取生产线每天电费数据
	 * @param lineId
	 * @param belongDate
	 */
	private void saveEquChargeDay(int equId,Date belongDate){
		try{
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd");
				List<ChargeConfig>configs=chargeConfigDao.findDistinctChargesName();
				if(configs.size()!=0){
					for(ChargeConfig charge:configs){
						ChargeDay day=chargeDayDao.getChargeDayByDate(equId, charge.getChargeName(),sdft.format(belongDate),"N");
						boolean isNew=false;
						if(day==null){
							day=new ChargeDay();
							day.setEquId(equId);
							day.setBelongDate(sdft.parse(sdft.format(belongDate)));
							day.setChargeName(charge.getChargeName());
							isNew=true;
						}
						//每天开始时间默认为0点，0点30分表示从0点开始算起
						String startDate=sdft.format(belongDate)+" "+"00:00:00";
						String endDate=sdf.format(belongDate);
						List<ChargeMin> list=chargeMinDao.getChargeMinsByDate(equId, startDate, endDate,charge.getChargeName(),"N");
						double relativeComp=0.0;double charges=0.0;
						if(list.size()!=0){
							for(ChargeMin min:list){
								relativeComp+=min.getRelativeComp();
								charges+=min.getCharge();
							}
						}
						day.setRelativeComp(relativeComp);
						day.setCharge(charges);
						if(isNew){
							chargeDayDao.save(day);
						}else{
							chargeDayDao.update(day);
					}
			}
		}
	} catch (ParseException e1) {
			e1.printStackTrace();
  }
}
}
